﻿Public Class frmPoOrder
    Dim Mysql As New ClsSQLhelper
    Dim MyWHIS As New cls_whisframework
    Dim Strsql As String = ""
    Dim On_Load As Boolean = True
#Region "Function"
    Sub Load_Orderstatus()
        Try
            Dim DTOrderstatus As New DataTable
            Strsql = "Select * from orderstatus where statustype = pr"
            DTOrderstatus = Mysql.GetMYSQLDataTable(Strsql, "orderstatus")
            With Me.CboOrderStatus
                .DataSource = DTOrderstatus
                .DisplayMember = "orderstatus"
                .ValueMember = "orderstatusid"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Orderstatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub
    'Sub Load_CboItem(ByVal MyCombobox As Custom_DGVComboBoxCol)
    '    Dim DS_item As New DataSet
    '    Strsql = "Select * from item where siteid = " & Me.CboSite.SelectedValue & " order by itemname"
    '    DS_item = Mysql.GetMYSQLDataset(Strsql, "DSItem", "Item")
    '    With MyCombobox
    '        .DataPropertyName = "item"
    '        .HeaderText = "item"
    '        .Name = "item"
    '        .ReadOnly = False
    '        .Visible = True
    '        .Width = 150
    '    End With
    '    If DS_item.Tables(0).Rows.Count Then
    '        For i = 0 To DS_item.Tables(0).Rows.Count - 1
    '            MyCombobox.Items.Add(DS_item.Tables(0).Rows(i).Item("itemname"))
    '        Next
    '    End If
    '    DS_item.Dispose()
    'End Sub
    'Sub Load_CboUOM(ByVal myCombobox As Custom_DGVComboBoxCol)
    '    Dim DS_UOM As New DataSet
    '    Strsql = "select * from itemunitofmeasure left join siteitemunit on unitofmeasureid = unitid where itemid = "
    '    DS_UOM = Mysql.GetMYSQLDataset(Strsql, "DSUOM", "itemunitofmeasure")
    '    With myCombobox
    '        .DataPropertyName = "UOM"
    '        .HeaderText = "unitofmeasure"
    '        .ReadOnly = False
    '        .Visible = True
    '        .Width = 150
    '    End With
    '    If DS_UOM.Tables(0).Rows.Count Then
    '        For i = 0 To DS_UOM.Tables(0).Rows.Count - 1
    '            myCombobox.Items.Add(DS_UOM.Tables(0).Rows(i).Item("unitofmeasure"))
    '        Next
    '    End If
    '    DS_UOM.Dispose()
    'End Sub

    Sub Load_CboSite()
        Try
            With Cbo_site
                .DataSource = MyWHIS.Get_DTSite
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_CboSite"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
       
        'DS_Site.Dispose()
    End Sub
    Sub Load_CboOrderStatus()
        Try
            With CboOrderStatus
                Dim DTOrderStatus As New DataTable
                Strsql = "Select orderstatusid,orderstatus from orderstatus"
                DTOrderStatus = Mysql.GetMYSQLDataTable(Strsql, "orderstatus")
                .DataSource = DTOrderStatus
                .DisplayMember = "orderstatus"
                .ValueMember = "orderstatusid"
                .SelectionStart = 0
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_CboOrderStatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
       
    End Sub

    Sub Load_Data(Optional ByVal Ordernumber As String = "", Optional ByVal ShowComplete As Boolean = False, Optional ByVal ShowCancel As Boolean = False, Optional ByVal ShowNotApproved As Boolean = False)
        Try
            Dim DS As New DataSet
            Strsql = "select orderdate,ordernumber,site.site,orderedby,departments.depname as depname,reason,orderstatus.orderstatus,yearbudget from poorder "
            Strsql = Strsql & " left join departments on poorder.department = departments.depcode "
            Strsql = Strsql & " left join site on poorder.site= site.siteid "
            Strsql = Strsql & " left join orderstatus on poorder.orderstatus= orderstatus.orderstatusid"
            Strsql = Strsql & " where orderstatus.statustype = 'po' and poorder.site = " & My.Settings.CurrentSite & " and poorder.orderstatus in  (0 "
            If ShowComplete = True Then Strsql = Strsql & ",1"
            If ShowCancel = True Then Strsql = Strsql & " ,2"
            If ShowNotApproved = True Then Strsql = Strsql & ",3"
            Strsql = Strsql & ")"
            Dim DTProrder As New DataTable
            DTProrder = Mysql.GetMYSQLDataTable(Strsql, "poorder")

            If Ordernumber <> "" Then
                Strsql = Strsql & " and ordernumber = " & Ordernumber
            End If
            Strsql = Strsql & " order by ordernumber desc"
            DS = Mysql.GetMYSQLDataset(Strsql, "DSpo_order", "po_order")
            Pn_Framework.Set_GridSetting(GridOrder, DS)
            With GridOrder
                .Columns("orderdate").HeaderText = "วันที่ขอ"
                .Columns("orderdate").DataPropertyName = "orderdate"
                .Columns("ordernumber").HeaderText = "เลขที่"
                .Columns("ordernumber").DataPropertyName = "ordernumber"
                .Columns("site").HeaderText = "คลังวัสดุ"
                .Columns("site").Width = 200
                .Columns("site").DataPropertyName = "site"
                .Columns("orderedby").HeaderText = "โดย"
                .Columns("depname").HeaderText = "แผนก"
                .Columns("depname").Width = 200
                .Columns("depname").DataPropertyName = "depname"
                .Columns("reason").HeaderText = "เหตุผล"
                .Columns("reason").DataPropertyName = "reason"
                .Columns("yearbudget").HeaderText = "ปีงบประมาณ"
                .Columns("yearbudget").DataPropertyName = "yearbudget"
                .Columns("orderstatus").HeaderText = "สถาณะ"
                .Columns("orderstatus").DataPropertyName = "orderstatus"
            End With
            DS.Dispose()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
#End Region

    Private Sub frmOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        EventLog1.Source = "WHIS"
        EventLog1.Log = "WHIS"
        Load_CboSite()
        Load_CboOrderStatus()
        Load_data()
        '  On_Load = False
    End Sub
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        FrmPoDetail.NewEntry_State = True
        FrmPoDetail.MdiParent = frmmain
        FrmPoDetail.Show()
    End Sub
    Private Sub GridOrder_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Try
            Me.Hide()
            Dim DS_LockedPRorder As New DataSet

            With Me.GridOrder
                ' LockStatus = False

                Dim DT_PO As New DataTable
                Dim MyOrdernumber As Integer = .Rows(e.RowIndex).Cells(0).Value
                '    FrmPoDetail.TxtPONumber.Text = .Rows(e.RowIndex).Cells(0).Value

                'Validate Order Status
                Strsql = "Select Orderstatus,Lockedby,lockeddatetime,lockedbycomputer from poorder where ordernumber = " & MyOrdernumber
                DT_PO = Mysql.GetMYSQLDataTable(Strsql, "poorder")
                If DT_PO.Rows(0).Item(0).ToString = 2 Then
                    MsgBox("คำสั่งซื้อนี้ได้รับของเรียบร้อยแล้ว ไม่สามารถแก้ไข้รายละเอียดได้", MsgBoxStyle.Exclamation, "คำเตือน")
                End If

                'Validate Locked Status

                If (DT_PO.Rows(0).Item(1).ToString <> "") Then
                    MsgBox("รายการขออนุมัติซื้อกำลังใช้งานโดย " & DT_PO.Rows(0).Item(1).ToString & " ตั้งแต่ " & DT_PO.Rows(0).Item(1).ToString & " ที่เครื่อง " & DT_PO.Rows(0).Item(2).ToString, MsgBoxStyle.Exclamation, "คำเตือน")
                    'Me.LockStatus = True
                Else
                    Strsql = "Update poorder set Lockedby = '" & My.Settings.Currentuser & "',lockeddatetime = '" & Pn_Framework.MysqlDateTimeFormat(Now) & "',lockedbycomputer = '" & My.Settings.CurrentComputer & "' where ordernumber = " & MyOrdernumber
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql.MySQLExecute(Strsql)

                End If
            End With
            FrmPoDetail.MdiParent = frmmain
            FrmPoDetail.Show()

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrder_CellDoubleClick"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Private Sub CboOrderStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If On_Load = False Then Load_Data()
    End Sub



    'Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    If Me.GridOrder.RowCount = 0 Then Exit Sub
    '    If (MsgBox("คุณต้องการลบ Order " & Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(0).Value & " นี้ใช่หรือไม่ ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No) Then Exit Sub
    '    Strsql = "Delete from podetail where ordernumber = " & Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(0).Value
    '    Mysql.MySQLExecute(Strsql)

    '    Strsql = "Delete from poorder where ordernumber = " & Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(0).Value
    '    Mysql.MySQLExecute(Strsql)
    '    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
    '    Load_Data()

    'End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        FrmPoDetail.NewEntry_State = False
        FrmPoDetail.TxtOrderNumber.Text = Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(1).Value
        FrmPoDetail.MdiParent = frmmain
        FrmPoDetail.Show()
    End Sub

    Private Sub GridOrder_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrder.CellContentClick

    End Sub

    Private Sub TxtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            Load_Data(Me.TxtSearch.Text)
        End If
    End Sub

    Private Sub TxtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSearch.TextChanged

    End Sub

    Private Sub chkShowComplete_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowComplete.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub

    Private Sub chkShowCancel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowCancel.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub

    Private Sub chkShowNotapproved_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowNotapproved.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub

    Private Sub GridOrder_CellDoubleClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrder.CellDoubleClick
        FrmPoDetail.NewEntry_State = False
        FrmPoDetail.TxtOrderNumber.Text = Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(1).Value
        FrmPoDetail.MdiParent = frmmain
        FrmPoDetail.Show()
    End Sub
End Class